﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ExtractorEditor : Form, IComponentEditor
    {
        private object ownerActivityNode;
        private ExtractionComponent _Extractor;

        /// <summary>
        /// ctor
        /// </summary>
        public ExtractorEditor()
        {
            InitializeComponent();
        }

        #region IComponentEditor Members

        ///<summary>
        ///</summary>
        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return ownerActivityNode; }
            set { ownerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(ExtractionComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this.ownerActivityNode = activityNode;
            this._Extractor = (ExtractionComponent) wfComponent;
            List<int> fys = new List<int>();
            if (this._Extractor.FiscalYears != null && this._Extractor.FiscalYears.Length > 0)
            {
                fys.AddRange(this._Extractor.FiscalYears);
            }
            if(this.clb_FiscalYears.Items.Count>0)
            {
                for (int i = 0; i < this.clb_FiscalYears.Items.Count; i++)
                {
                    int fy = int.Parse(this.clb_FiscalYears.Items[i].ToString());
                    if (fys.Contains(fy))
                    {
                        this.clb_FiscalYears.SetItemChecked(i, true);
                    }
                }
            }

            if(this.clb_ActivityCodes.Items.Count>0 && this._Extractor.ActivityCodes !=null && this._Extractor.ActivityCodes.Length>0)
            {
                for (int i = 0; i < this.clb_ActivityCodes.Items.Count; i++)
                {
                    this.clb_ActivityCodes.SetItemChecked(i,false);
                }

                foreach(string activityCode in this._Extractor.ActivityCodes)
                {
                    for(int i=0;i<this.clb_ActivityCodes.Items.Count;i++)
                    {
                        if(this.clb_ActivityCodes.Items[i].ToString()==activityCode)
                        {
                            this.clb_ActivityCodes.SetItemChecked(i,true);
                            break;
                        }
                    }
                }
            }

            if(this.clb_ApplTypeCodes.Items.Count>0 && this._Extractor.ApplTypeCodes !=null && this._Extractor.ApplTypeCodes.Length>0)
            {
                for(int i=0;i<this.clb_ApplTypeCodes.Items.Count;i++)
                {
                    this.clb_ApplTypeCodes.SetItemChecked(i,false);
                }
                foreach(string applTypeCode in this._Extractor.ApplTypeCodes)
                {
                    for(int i=0;i<this.clb_ApplTypeCodes.Items.Count;i++)
                    {
                        if(this.clb_ApplTypeCodes.Items[i].ToString()==applTypeCode)
                        {
                            this.clb_ApplTypeCodes.SetItemChecked(i,true);
                            break;
                        }
                    }
                }
            }

            if(this.clb_DocTypeCodes.Items.Count>0 && this._Extractor.DocTypeCodes !=null && this._Extractor.DocTypeCodes.Length>0)
            {
                for(int i=0;i<this.clb_DocTypeCodes.Items.Count;i++)
                {
                    this.clb_DocTypeCodes.SetItemChecked(i,false);
                }
                foreach(string docTypeCode in this._Extractor.DocTypeCodes)
                {
                    for(int i=0;i<this.clb_DocTypeCodes.Items.Count;i++)
                    {
                        if(this.clb_DocTypeCodes.Items[i].ToString()==docTypeCode)
                        {
                            this.clb_DocTypeCodes.SetItemChecked(i,true);
                            break;
                        }
                    }
                }
            }

            this.txt_LogFilePath.Text = this._Extractor.LogFilePath;
            this.txt_ErrorFilePath.Text = this._Extractor.ErrorFilePath;
            this.txt_DocFolder.Text = this._Extractor.LocalDocRootFolderPath;
        }

        public void SaveSettings()
        {
            List<int> fys = new List<int>();
            for (int i = 0; i < this.clb_FiscalYears.Items.Count; i++)
            {
                if (this.clb_FiscalYears.GetItemChecked(i))
                {
                    fys.Add(int.Parse(this.clb_FiscalYears.Items[i].ToString()));
                }
            }
            this._Extractor.FiscalYears = fys.ToArray();
            List<string> activityCodes=new List<string>();
            for(int i=0;i<this.clb_ActivityCodes.Items.Count;i++)
            {
                if(this.clb_ActivityCodes.GetItemChecked(i))
                {
                    activityCodes.Add(this.clb_ActivityCodes.Items[i].ToString());
                }
            }
            this._Extractor.ActivityCodes = activityCodes.ToArray();
            List<string> applTypeCodes=new List<string>();
            for(int i=0;i<this.clb_ApplTypeCodes.Items.Count;i++)
            {
                if(this.clb_ApplTypeCodes.GetItemChecked(i))
                {
                    applTypeCodes.Add(this.clb_ApplTypeCodes.Items[i].ToString());
                }
            }
            this._Extractor.ApplTypeCodes = applTypeCodes.ToArray();
            List<string> docTypeCodes=new List<string>();
            for(int i=0;i<this.clb_DocTypeCodes.Items.Count;i++)
            {
                if(this.clb_DocTypeCodes.GetItemChecked(i))
                {
                    docTypeCodes.Add(this.clb_DocTypeCodes.Items[i].ToString());
                }
            }
            this._Extractor.DocTypeCodes = docTypeCodes.ToArray();

            this._Extractor.LogFilePath = this.txt_LogFilePath.Text;
            this._Extractor.ErrorFilePath = this.txt_ErrorFilePath.Text;
            this._Extractor.LocalDocRootFolderPath = this.txt_DocFolder.Text;
        }

        public TestResult Test()
        {
            return new TestResult(true,"Succeed!");
        }

        #endregion

        private void btn_BrowseLog_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg=new SaveFileDialog();
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_LogFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseErr_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt_ErrorFilePath.Text = dlg.FileName;
            }
        }

        private void ExtractorEditor_Load(object sender, EventArgs e)
        {
            this.clb_FiscalYears.Items.Clear();
            this.clb_ActivityCodes.Items.Clear();
            this.clb_ApplTypeCodes.Items.Clear();
            this.clb_DocTypeCodes.Items.Clear();
            string connStr = ConfigurationManager.ConnectionStrings["Enumeration"].ConnectionString;
            SqlConnection conn=new SqlConnection(connStr);
            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select distinct FY from DocsMetaData where FY is not null order by FY";
                SqlDataReader reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    int fy = int.Parse(reader["FY"].ToString());
                    this.clb_FiscalYears.Items.Add(fy);
                }
                reader.Close();

                cmd.CommandText = "select distinct Activity_Code from Docs_T where Activity_Code is not null order by Activity_Code";
                reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    string activityCode = reader["Activity_Code"].ToString();
                    this.clb_ActivityCodes.Items.Add(activityCode);
                }
                reader.Close();

                cmd.CommandText = "select distinct Appl_Type_Code from Docs_T where Appl_Type_Code is not null order by Appl_Type_Code";
                reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    string applTypeCode = reader["Appl_Type_Code"].ToString();
                    this.clb_ApplTypeCodes.Items.Add(applTypeCode);
                }
                reader.Close();

                cmd.CommandText = "select distinct Doc_Type_Code from Docs_T where Doc_Type_Code is not null order by Doc_Type_Code";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string docTypeCode = reader["Doc_Type_Code"].ToString();
                    this.clb_DocTypeCodes.Items.Add(docTypeCode);
                }
                reader.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent!=null)
            {
                this.UpdateComponentSettingEvent(this.ownerActivityNode, this._Extractor);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            ExtractionTester tester=new ExtractionTester();
            tester.Show();
        }

        private void btn_BroseDocFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_DocFolder.Text = dlg.SelectedPath;
            }
        }

        private void ck_AllFY_CheckedChanged(object sender, EventArgs e)
        {
            if (this.clb_FiscalYears.Items.Count > 0)
            {
                bool allChecked = this.ck_AllFY.Checked;
                for (int i = 0; i < this.clb_FiscalYears.Items.Count; i++)
                {
                    this.clb_FiscalYears.SetItemChecked(i, allChecked);
                }
            }
        }

        private void ck_AllApplTypes_CheckedChanged(object sender, EventArgs e)
        {
            if (this.clb_ApplTypeCodes.Items.Count > 0)
            {
                bool allChecked = this.ck_AllApplTypes.Checked;
                for (int i = 0; i < this.clb_ApplTypeCodes.Items.Count; i++)
                {
                    this.clb_ApplTypeCodes.SetItemChecked(i, allChecked);
                }
            }
        }

        private void ck_AllDocTypes_CheckedChanged(object sender, EventArgs e)
        {
            if (this.clb_DocTypeCodes.Items.Count > 0)
            {
                bool allChecked = this.ck_AllDocTypes.Checked;
                for (int i = 0; i < this.clb_DocTypeCodes.Items.Count; i++)
                {
                    this.clb_DocTypeCodes.SetItemChecked(i, allChecked);
                }
            }
        }

        private void ck_AllActivityCodes_CheckedChanged(object sender, EventArgs e)
        {
            if (this.clb_ActivityCodes.Items.Count > 0)
            {
                bool allChecked = this.ck_AllActivityCodes.Checked;
                for (int i = 0; i < this.clb_ActivityCodes.Items.Count; i++)
                {
                    this.clb_ActivityCodes.SetItemChecked(i, allChecked);
                }
            }
        }

        //private void btn_AdvTest_Click(object sender, EventArgs e)
        //{
        //    PdfLib.Test.MainTester mainTester =new MainTester();
        //    mainTester.Show();
        //}

    
    }
}
